package dblayer

import (
	mydb "easycharts/db/mysql"
	"easycharts/utils/errmsg"
	"fmt"
)


func ScryptPw(passwd string) string {
	str := []byte(passwd)
    for i := 0; i < len(str); i++ {
		str[i] += 1
    }

	return string(str)
}


func CheckLogin(username string, passwd string) int {
	stmt, err := mydb.DBConn().Prepare("select password from admin where" +
		" uid= ? limit 1")
	if err != nil {
		fmt.Println(err.Error())
		return errmsg.ERROR
	}

	var pwd []byte
	err = stmt.QueryRow(username).Scan(&pwd)
	if err != nil {
		fmt.Println("username seem to not exist")
		return errmsg.ERROR
	}
	if len(pwd) > 0 && string(pwd) == ScryptPw(passwd) {
		return errmsg.SUCCSE
	}
	return errmsg.ERROR
}

